import Taro from "@tarojs/taro";

// 记录搜索信息
export function saveSearchWord(key, keyword) {
  let list = Taro.getStorageSync(key) || [];
  if (!keyword.trim()) return;
  list.unshift(keyword);
  list = Array.from(new Set(list));
  if (list.length > 10) {
    list.pop()
  }
  Taro.setStorageSync(key, list);
}

export function parseUrlToObj(url) {
  let obj = {}
  let search = decodeURIComponent(url.substring(url.lastIndexOf("?") + 1));
  let reg = /([^?&=]+)=([^?&=]*)/g;
  search.replace(reg, (rs, $1, $2) => {
    let name = decodeURIComponent($1);
    let val = decodeURIComponent($2);
    val = String(val);
    obj[name] = val;
    return rs;
  });
  return obj;
}



export function timePickerList () {
  let years = [], months = []
  for ( let i = 1; i <= 3000; i++ ) {
    years[i - 1] = i
  }
  for ( let i = 1; i <= 12; i++ ) {
    months[i - 1] = i < 10? `0${i}` : i
  }
  return [years, months]
}
